﻿
Public Class UCTinDang
    Public Sub ganKH(ByVal ID As String, ByVal tenKH As String, ByVal CMND As String, ByVal SDT As String)
        Me.txtCMND.Text = CMND
        Me.txtMaKH.Text = ID
        Me.txtSDT.Text = SDT
        Me.txtTenKH.Text = tenKH
    End Sub
    Private Sub UCTinDang_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        dtNgayDang.Text = DateTime.Today()
        LoadcmbQuan()
        LoadcmbDonVi()
        LoadcmbLoai()
        cmbDonVi.SelectedValue = "VND"
    End Sub

    Dim connect As New connect()

    Private Function TaoMaTin() As String
        Dim yy As Integer = DateTime.Today.Year Mod 100
        Dim mm As Integer = DateTime.Today.Month
        Dim dd As Integer = DateTime.Today.Day

        Dim ngay As String = yy.ToString() + mm.ToString("00") + dd.ToString("00")
        Dim dt As DataTable = connect.GetData("select * from TINDANG where MATD like '" + ngay + "%'")
        Dim stt As Integer = dt.Rows.Count() + 1
        Dim str_stt As String = stt.ToString("000")
        Dim maTin As String = ngay + str_stt
        Dim check As Boolean = False
        While (check = False)
            Dim dt2 As DataTable = connect.GetData("select * from TINDANG where MATD = '" + maTin + "'")
            If (dt2.Rows.Count() > 0) Then
                stt = stt + 1
                str_stt = stt.ToString("000")
                maTin = ngay + str_stt
                Dim dt3 As DataTable = connect.GetData("select * from TINDANG where MATD ='" + maTin + "'")
                If (dt3.Rows.Count() > 0) Then
                    check = False
                Else
                    check = True
                End If
            Else
                check = True
            End If
        End While
        Return maTin
    End Function
    Private Sub LoadcmbDonVi()
        Dim ds As New DataSet()
        ds = connect.LoadDonVi("select MADV,TENDV from DONVI")
        With cmbDonVi
            .DataSource = ds.Tables("DONVI")
            .DisplayMember = "TENDV"
            .ValueMember = "MADV"
        End With
    End Sub
    Private Sub LoadcmbQuan()
        Dim ds As New DataSet()
        ds = connect.LoadQuan("select MAQUAN,TENQUAN from QUAN")
        With cmbQuan
            .DataSource = ds.Tables("QUAN")
            .DisplayMember = "TENQUAN"
            .ValueMember = "MAQUAN"
        End With
    End Sub
    Private Sub LoadcmbLoai()
        Dim ds As New DataSet()
        ds = connect.LoadLoaiPhong("select MALP,TENLP from LOAIPHONG")
        With cmbLoai
            .DataSource = ds.Tables("LOAIPHONG")
            .DisplayMember = "TENLP"
            .ValueMember = "MALP"
        End With
    End Sub

    Private Sub btnChon_Click(sender As System.Object, e As System.EventArgs)
        Dim frm As New frmTimKH()
        frm.Show()

    End Sub

    Private Sub btnChon_Click_1(sender As System.Object, e As System.EventArgs) Handles btnChon.Click
        Dim frm As New frmTimKH
        frm.setflag(1)
        frm.ShowDialog()
       
    End Sub


    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        If (txtMaKH.Text = "") Then
            MessageBox.Show("Bạn chưa chọn khách hàng")
            Return
        End If
        If (txtTieuDe.Text = "") Then
            MessageBox.Show("Bạn chưa nhập tiêu đề")
            Return
        End If
        Dim maTin As String = TaoMaTin()
        Dim tieuDe As String = txtTieuDe.Text
        Dim ngayDang As String = dtNgayDang.Text
        Dim maLP As Integer = Convert.ToInt32(cmbLoai.SelectedValue)
        Dim maNV As String = frmQuanLy.LayMaNV()
        Dim maQuan As Integer = Convert.ToInt32(cmbQuan.SelectedValue)
        Dim maKH As String = txtMaKH.Text
        Dim dienTich As Integer = Convert.ToInt32(txtDienTich.Text)
        Dim sucChua As Integer = Convert.ToInt32(txtSucChua.Text)
        Dim ganCho As String = Convert.ToString(cbGanCho.Checked)
        Dim coInternet As String = Convert.ToString(cbCoInternet.Checked)
        Dim ganBus As String = Convert.ToString(cbGanBus.Checked)
        Dim giaTien As Integer = Convert.ToInt32(txtGiaTien.Text)
        Dim maDv As String = cmbDonVi.SelectedValue.ToString().Trim()
        Dim daThue As String = "False" 'Convert.ToString(cbDaThue.Checked)
        Dim biXoa As String = "False" 'Convert.ToString(cbBiXoa.Checked)

        Dim sqlthem As String = "insert into TINDANG(MATD,TIEUDE,NGAYDANG,MALP,MANV,MAQUAN,MAKH,DIENTICH,SUCCHUA,GANCHO,INTERNET,GANBUS,GIATIEN,MADV,DATHUE,BIXOA) values('" + maTin + "',N'" + tieuDe + "','" + ngayDang + "'," + maLP.ToString() + ",'" + maNV + "'," + maQuan.ToString() + ",'" + maKH + "'," + dienTich.ToString() + "," + sucChua.ToString() + ",'" + ganCho + "','" + coInternet + "','" + ganBus + "'," + giaTien.ToString() + ",'" + maDv + "','" + daThue + "','" + biXoa + "')"
        connect.ExecSql(sqlthem)
        MessageBox.Show("Thêm thành công")
        LamMoi()
    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        LamMoi()
    End Sub
    Private Sub LamMoi()
        txtTieuDe.Text = ""
        txtDienTich.Text = ""
        txtGiaTien.Text = ""
        txtSucChua.Text = ""
        dtNgayDang.Text = DateTime.Today()
        cmbDonVi.SelectedValue = "VND"
        cmbQuan.SelectedValue = 1
        cmbLoai.SelectedValue = 1
        cbBiXoa.Checked = False
        cbCoInternet.Checked = False
        cbDaThue.Checked = False
        cbGanBus.Checked = False
        cbGanCho.Checked = False
    End Sub
End Class
